﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using myBoard.Framework;
using System.Reflection;
using myBoard.Tools;
using System.Diagnostics;
using myTunes.API;
using System.Windows.Threading;
using System.Threading;

namespace myTunes
{
    /// <summary>
    /// Interaction logic for Magnet.xaml
    /// </summary>
    public partial class Magnet : UserControl , IMagnet, IFlip, IRequireGlobalHook
    {
        bool _isSettingVolume;
        iTunesPlayer _player;

        public Magnet()
        {
            InitializeComponent();
            _flipToBackBt.Click += new RoutedEventHandler(_flipToBackBt_Click);
            _flipToFrontBt.Click += new RoutedEventHandler(_flipToFrontBt_Click);
            Connector.MagnetDropped += new Connector.MagnetDroppedHandler(Connector_MagnetDropped);
        }

        void Connector_MagnetDropped(IMagnet droppedMagnet)
        {
            if (droppedMagnet.StoreID == StoreID)
            {
                _player = this.Resources["iTunesPlayerDataSource"] as iTunesPlayer;
                _player.InitPlayer();                
                _volumeRing.MouseLeftButtonDown += new MouseButtonEventHandler(_volumeRing_MouseLeftButtonDown);
                _volumeThumb.MouseLeftButtonDown += new MouseButtonEventHandler(_volumeThumb_MouseLeftButtonDown);                
            }
        }

        void _volumeThumb_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
            _isSettingVolume = true;
        }

        void _volumeRing_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
            _isSettingVolume = true;
        }

        #region IRequireGlobalHook Members

        public void HandleGHKeyboardActivity(Key key)
        {
            
        }

        public void HandleGHMouseActivity(MouseButton mouseButton, System.Drawing.Point screenPt)
        {
            double angle=0;
            if (Mouse.LeftButton == MouseButtonState.Pressed && _isSettingVolume)
            {
                Point pt = _volumeCenterPoint.PointFromScreen(new Point(screenPt.X, screenPt.Y));
                pt = new Point(pt.X, pt.Y * -1);
                angle = Math.Atan2(pt.Y - 1, pt.X);
                if (angle < 0)
                    angle = 2 * Math.PI + angle;
                angle = -((angle * 180) / Math.PI) + 90;
                ((RotateTransform)_volumeThumbContainer.RenderTransform).Angle = angle;

                this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new ThreadStart(() =>
                {
                    if (_player != null && _player.IsInitialized && !_player.IsMute)
                    {
                        angle -= 90;
                        angle = Math.Abs(angle);
                        _player.Volume = Convert.ToInt32(angle / 360 * 100);
                    }
                })); 
            }
            else
                _isSettingVolume = false;
                       
        }

        #endregion

        void _flipToFrontBt_Click(object sender, RoutedEventArgs e)
        {
            FlipRequested(MagnetSides.Front);
        }

        void _flipToBackBt_Click(object sender, RoutedEventArgs e)
        {
            FlipRequested(MagnetSides.Back);
        }

        #region IFlip Members

        public event OnFlipRequested FlipRequested;

        public Panel GetBackSide()
        {
            return _back;
        }

        public Panel GetFrontSide()
        {
            return _front;
        }

        #endregion

        #region IMagnet Members

        public BitmapImage Icon
        {
            get 
            {
                Assembly asm = Assembly.GetExecutingAssembly();
                return ResourcesAccess.GetBitmapImageFromResource(asm,(string.Format("myTunes.Images.{0}.png", "Icon")));
            }
        }

        public string MagnetName
        {
            get 
            {
                return "myTunes";
            }
        }

        public string StoreID
        {
            get 
            {
                return "{A11E4F23-BEC4-4c5f-9C54-4B6C3F3DBDD9}";
            }
        }

        public void Terminate()
        {
            Connector.MagnetDropped -= new Connector.MagnetDroppedHandler(Connector_MagnetDropped);
            IntPtr h = User32.FindWindowEx(IntPtr.Zero, IntPtr.Zero, "iTunes", IntPtr.Zero);

            if (!User32.IsWindowVisible(h))
            {
                User32.ShowWindow(h, User32.WindowStatus.SW_SHOW);
                User32.MakeNormal(h);
            }
        }

        public BitmapImage Thumbnail
        {
            get 
            {
                Assembly asm = Assembly.GetExecutingAssembly();
                return ResourcesAccess.GetBitmapImageFromResource(asm, (string.Format("myTunes.Images.{0}.png", "Thumb")));
            }
        }

        #endregion

        
    }
}
